package com.gopay.bis.web.controller.ebank;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.gopay.bis.domain.order.OrderInfo;
import com.gopay.common.Result;
import com.gopay.common.constants.bank.BankConstants;
import com.gopay.common.constants.bank.BisBankBusinessProd;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.bocom.ebank.pay.BocomB2cMessage;

@Controller
@Scope("prototype")
public class BocomB2cServerRes extends BankResBase {
	
	private static Log logger = LogFactory.getLog(BocomB2cServerRes.class);

	@RequestMapping(value = "/bocomB2cServerRes")
	protected ModelAndView execute(HttpServletRequest request,HttpServletResponse response) {
		logger.error("--------------- BocomB2cServerRes bankback ---------------");
		return super.execute(request, response);
	}

	@Override
	protected BaseMessage buildMessage(HttpServletRequest request) {		
		BocomB2cMessage message = new BocomB2cMessage();
		message.setBank(BankConstants.BANK_LOGO_BOCOM);
		String notifyMsg = request.getParameter("notifyMsg");
		int lastIndex = notifyMsg.lastIndexOf("|");
		String signMsg = notifyMsg.substring(lastIndex + 1, notifyMsg.length()); // 获取签名信息
		String srcMsg = notifyMsg.substring(0, lastIndex + 1); // 获取返回明文
		List list = processSrcMsg(srcMsg);
		message.setOrderId((String) list.get(1));
		message.setBankOrderId((String) list.get(1));
		message.setRetCode((String) list.get(9));
		message.setErrorMessage("");
		message.setFront(false);
		message.setAmount(new BigDecimal((String) list.get(2)));		
		message.setPlainMessage(srcMsg);
		message.setSignMessage(signMsg);
        //银行业务产品00个人网银支付
        message.setBankBusProd(BisBankBusinessProd._00);
		return message;
	}

	@Override
	protected OrderInfo buildOrderInfo(HttpServletRequest request,Result result) {
		BocomB2cMessage message =(BocomB2cMessage)result.getModel("res");
		OrderInfo orderInfo = new OrderInfo(); 
		if(message!=null){
			if(message.getRetCode().equals("1")){
				orderInfo.setOrderId(message.getOrderId());
			    orderInfo.setAmt(message.getAmount().toString());
			    orderInfo.setNotifyNum("1");
		        orderInfo.setPayStatus("00");
		        orderInfo.setRetCode("00");
		        orderInfo.setRetMsg("");
			}else{
				 orderInfo.setOrderId(message.getOrderId());
		         orderInfo.setAmt(message.getAmount().toString());
		         orderInfo.setNotifyNum("1");
		         orderInfo.setPayStatus("01");
		         orderInfo.setRetCode("01");
		         orderInfo.setRetMsg("");
			}
			orderInfo.setBankName("bocom");
	   }
		return orderInfo;
	}

	/**
	 * 解析返回报文
	 * 
	 * @param srcMsg
	 * @return
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private List processSrcMsg(String srcMsg) {
		StringTokenizer stName = new StringTokenizer(srcMsg, "|");
		List list = new ArrayList();
		while (stName.hasMoreTokens()) {
			String value = (String) stName.nextElement();
			if (value.equals(""))
				value = "&nbsp;";
			list.add(value);
		}
		return list;
	}

	@Override
	protected boolean checkParameters(HttpServletRequest request) {
		if(StringUtils.isEmpty(request.getParameter("notifyMsg"))){
        	return false;
        }
		return true;
	}

}
